<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>公告通知管理</title>
    <#include "/common/link.ftl">
</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="notice"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>公告通知管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 20px 0px 0px 10px">
                    <form class="form-inline" id="searchForm" action="/notice2/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <input type="hidden" name="admin" value="${employee.admin}"/>
                        <div class="form-group">
                            <label>创建日期查询：</label>
                            <input placeholder="请输入开始日期" type="text" class="form-control input-date" name="startDate"
                                   value="${(qo.startDate?string('yyyy-MM-dd HH:mm:ss'))!}"/> -
                            <input placeholder="请输入结束日期" type="text" class="form-control input-date" name="endDate"
                                   value="${(qo.endDate?string('yyyy-MM-dd HH:mm:ss'))!}"/>
                        </div>

                        <div class="form-group">
                            <label>公告级别：</label>
                            <select class="form-control" name="level" id="level">
                                <option value="">请选择级别</option>
                                <option value="0">紧急</option>
                                <option value="1">重要</option>
                                <option value="2">普通</option>
                            </select>
                        </div>
                        <script>
                            $("#level").val()
                        </script>
                        <div class="form-group">
                            <label>阅读状态：</label>
                            <select class="form-control" name="msgStatus">
                                <option value="">全部</option>
                                <option value="0">未读</option>
                                <option value="1">已读</option>
                            </select>
                        </div>

                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>
                            查询
                        </button>
                        <#if employee.admin>
                            <a class="btn btn-success btn-input">
                                <span class="glyphicon glyphicon-plus"></span> 添加
                            </a>
                        </#if>
                    </form>

                </div>
                <div class="box-body table-responsive">

                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>公告标题</th>
                            <th>创建人</th>
                            <th>创建时间</th>
                            <th>公告级别</th>
                            <th>是否已读</th>
                            <th>发布状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <#list pageInfo.list as notice>

                                <tr>
                                    <td>${notice_index+1}</td>
                                    <td>${notice.title}</td>
                                    <td>${notice.employee.username}</td>
                                    <td>${(notice.createTime?string('yyyy-MM-dd HH:mm:ss'))!}</td>
                                    <td>${(notice.findNoticeLevel())!}</td>

                                    <#if notice.employeeNotice.msgStatus>
                                        <td><span style="color:green">已读</span></td>
                                    <#else >
                                        <td><span style="color:red">未读</span></td>
                                    </#if>
                                    <td>${(notice.noticeStatus?string('已发布','未发布'))!}</td>
                                    <td>
                                        <#--href="/notice/show?noticeId=${notice.id}&employeeId=${notice.employee.id}" -->
                                        <a data-id='${notice.id}' class="btn btn-success btn-xs  btn-show">
                                            <span class="glyphicon glyphicon-phone-alt"></span> 查看
                                        </a>
                                        '

                                        <#if employee.admin>
                                            <a class="btn btn-primary btn-xs btn-input" id="edit"
                                               data-json='${notice.toNotice()}'>
                                                <span class="glyphicon glyphicon-pencil"></span> 编辑
                                            </a>
                                            <#if !notice.noticeStatus>
                                                <a class="btn btn-xs btn-info btn-push" data-id='${notice.id}'
                                                   data-status="1">
                                                    <span class="glyphicon glyphicon-share"></span> 发布
                                                </a>
                                            </#if>
                                        <#--为true时是已经发布，显示取消发布按钮-->
                                            <#if notice.noticeStatus>
                                                <a class="btn btn-xs btn-danger btn-remove" data-id='${notice.id}'
                                                   data-status="0">
                                                    <span class="glyphicon glyphicon-remove"></span> 取消
                                                </a>
                                            </#if>
                                        </#if>
                                    </td>
                                </tr>

                        </#list>
                        </tbody>
                    </table>
                    <#include "/common/page.ftl">
                </div>
            </div>
        </section>
    </div>
    <#include "/common/footer.ftl">
</div>

<!--模态框-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
            </div>

            <form id="editForm" action="/notice2/saveOrUpdate" method="post">
                <div class="modal-body">
                    <input type="hidden" name="id">
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">公告标题：</label>
                        <div class="col-sm-7">
                            <input type="text" class="form-control" placeholder="请输入公告标题" name="title"/>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">公告内容：</label>
                        <div class="col-sm-7">
                            <textarea type="text" class="form-control" rows="8"
                                      placeholder="请输入公告内容" name="info"></textarea>
                        </div>
                    </div>

                    <div class="form-group row">
                        <label class="col-sm-3 col-form-label">公告级别：</label>
                        <div class="col-sm-7">
                            <select class="form-control" name="level" id="level">
                                <#-- <#list noticeLevels as noticeLevel>
                                     <option value="${noticeLevel.getValue()}">${noticeLevel.getName()}</option>
                                 </#list>-->
                                <option value="">请选择级别</option>
                                <option value="0">普通</option>
                                <option value="1">重要</option>
                                <option value="2">紧急</option>
                            </select>

                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary btn-submit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    //点击查看按钮时，发送一个ajax到后端,修改状态
    $(".btn-show").click(function () {
        let $noticeId = $(this).data('id');

        //修改消息的状态
        $.post('/notice/show',{id:$noticeId},function (data) {
            if (data.success) {
                    window.location.href = '/notice/showInfo?id='+$noticeId;
            } else {
                Swal.fire(
                    data.msg,
                    '',
                    'error'
                )
            }
        })
    })

    $(".btn-input").click(function () {
        //拿到数据
        var $notice = $(this).data('json');
        if ($notice) {
            $("#editForm input[name=title]").val($notice.title);
            $("#editForm textarea[name=info]").val($notice.info)
            $("#editForm select[name=level]").val($notice.level)
            $("#editForm input[name=id]").val($notice.id)
        }

        $("#editModal").modal('show')
    })


    //点击保存按钮：提交模态框中的表单
    $("#editForm").ajaxForm(function (data) {
        if (data.success) {
            Swal.fire({
                text: '确认保存',
                icon: 'success',
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: '保存成功'
            }).then((result) => {
                window.location.href = '/notice2/list';
            })
        } else {
            Swal.fire(
                data.msg,
                '',
                'error'
            )
        }
    })
    //发布按钮
    $(".btn-push").click(function () {
        let $status = $(this).data('status');

        console.log($status);
        var $id = $(this).data('id');

        updateNoticeStatus($id, $status);
    })
    //取消发布按钮
    $(".btn-remove").click(function () {
        let $status = $(this).data('status');
        var $id = $(this).data('id');

        updateNoticeStatus($id, $status);
    })


</script>

</body>
</html>
